<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class User extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request){
        if($request->isAjax()){
            $name = $request->get('name','','trim');
            $admin_id = $request->get('admin_id',0,'intval');
            $page = $request->get('page',1,'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit',10,'intval');
            $user = Db('user');
            if ($name !== '') {
                $user->where('name|nickname', 'like', '%' . $name . '%');
            }
            if ($admin_id !== 0) {
                $user->where('admin_id', $admin_id);
            }
            $count = $user->count();
            $list = $user->order('user_id desc')->page($page,$limit)->select();
            return ['code' => 0, 'data' => $list,'count'=>$count];
        } else {
            return $this->fetch();
        }
    }

    public function userOrder(Request $request){
        if ($request->isAjax()) {
            $user_id = $request->param('id', 0, 'intval');
            $order = Db('order');
            $order->where('user_id',$user_id);
            $list = $order
                ->order('create_time desc')
                ->select();
            return ['code' => 0, 'data' => $list];
        } else {
            return $this->fetch();
        }
    }

    public function userProductOrder(Request $request){
        if ($request->isAjax()) {
            $user_id = $request->param('id', 0, 'intval');
            $order = Db('productOrder');
            $order->where('user_id',$user_id);
            $list = $order
                ->order('create_time desc')
                ->select();
            return ['code' => 0, 'data' => $list];
        } else {
            return $this->fetch();
        }
    }

    public function editUser(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $user_id = $request->post('user_id', 0, 'intval');
            $data['name'] = $request->post('name', '', 'trim');
            $data['phone'] = $request->post('phone', '', 'trim');
            $data['sex'] = $request->post('sex', 0, 'intval');
            $data['company'] = $request->post('company', '', 'trim');
            $data['position'] = $request->post('position', '', 'trim');
            $data['address'] = $request->post('address',  '', 'trim');
            $data['admin_id'] = $request->post('admin_id', 0, 'intval');
            if (false === Db('user')->where('user_id',$user_id)->update($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $user_id = $request->get('id', 0, 'intval');
            $info = Db('user')->where('user_id',$user_id)->find();
            $this->assign('info', $info);
            $admin = Db('admin')->where('type',2)->order('admin_id asc')->select();
            $this->assign('admin', $admin);
            return $this->fetch();
        }
    }
}
